﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LocalCharge.Calc.aspx.cs"
    Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <!--#include virtual="include/Head.ascx"-->
    <script type='text/javascript' src="assets/js/Carrs.js"></script>
    <script type='text/javascript' src="assets/js/Ports.js"></script>
    <script type="text/javascript">
        var sumBuyPriceUSD = 0;
        var sumSellNoVatUSD = 0;
        var sumSellWithVatUSD = 0;
        var sumBuyPriceVND = 0;
        var sumSellNoVatVND = 0;
        var sumSellWithVatVND = 0;
        function getUrlVars() {
            var vars = {};
            var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
                vars[key] = value;
            });
            return vars;
        }
        $().ready(function () {
            $('#txtCarr').focus().autocomplete(carrs, { matchContains: true });
            $('#txtPort').focus().autocomplete(ports, { matchContains: true });
            $('#txtRates').bind('keypress', function (e) {
                if (e.keyCode == 13) {
                    var ratVal = parseFloat($(this).val().replace(/,/g, ""));
                    var totalBuy = ratVal * sumBuyPriceUSD + sumBuyPriceVND;
                    var totalSellNoVAT = ratVal * sumSellNoVatUSD + sumSellNoVatVND;
                    var totalSellWithVAT = ratVal * sumSellWithVatUSD + sumSellWithVatVND;
                    $("#totalBuy").text(formatNumber(totalBuy));
                    $("#totalSellNoVAT").text(formatNumber(totalSellNoVAT));
                    $("#totalSellWithVAT").text(formatNumber(totalSellWithVAT));
                    $(this).val(formatNumber(ratVal));
                    e.preventDefault();
                }
            });

            $(".BuyPrice").live("blur", function () {
                var obj = $(this).parent().parent();
                var lcId = $(obj).attr("lcID");
                var fbuyprice = $(this).val();
                var fsellpricewithvat = fbuyprice * 1.05;
                var fsellpricenovat = fsellpricewithvat / 1.1;
                var objNoVaT = $(obj).children().find('.SellPriceNoVat');
                var objWithVaT = $(obj).children().find('.SellPriceWithVat');
                $(objNoVaT).val(fsellpricenovat.toFixed(2));
                $(objWithVaT).val(fsellpricewithvat.toFixed(2));
            });
            //VAT 10 %
            $(".SellPriceWithVat").live("blur", function () {
                var obj = $(this).parent().parent();
                var lcId = $(obj).attr("lcID");
                var fsellpricewithvat = $(this).val();
                var fsellpricenovat = fsellpricewithvat / 1.1;
                var fbuyprice = fsellpricewithvat / 1.05;
                var objBuyPrice = $(obj).children().find('.BuyPrice');
                var objNoVaT = $(obj).children().find('.SellPriceNoVat');
                $(objNoVaT).val(fsellpricenovat.toFixed(2));
                $(objBuyPrice).val(fbuyprice.toFixed(2));
            });
            $(":checkbox").live("change", function () {
                calcRate(this);
            });
            $("#checkAll").click(function () {
                var rschecked = this.checked;
                $('.cbCheckCalc').each(function (index, item) {
                    item.checked = rschecked;
                    calcRate(this);
                });
            });

        });
        function calcRate(othis) {
            var obj = $(othis).parent().parent();
            var fbuyprice = $(obj).children().find('.BuyPrice').val();
            var fsellnovat = $(obj).children().find('.SellPriceNoVat').val();
            var fsellwithvat = $(obj).children().find('.SellPriceWithVat').val();
            var currencyType = $(obj).children().find('.ddCurrencyType').val();
            //alert("fbuyprice="+fbuyprice+", fsellnovat="+fsellnovat+", fsellwithvat="+fsellwithvat+", type="+currencyType);
            if (othis.checked) {
                //Cong tong                    
                if (currencyType == 2) {
                    if (fbuyprice)
                        sumBuyPriceVND += parseFloat(fbuyprice.replace(/,/g, ""));
                    if (fsellnovat)
                        sumSellNoVatVND += parseFloat(fsellnovat.replace(/,/g, ""));
                    if (fsellwithvat)
                        sumSellWithVatVND += parseFloat(fsellwithvat.replace(/,/g, ""));
                }
                else {
                    if (fbuyprice)
                        sumBuyPriceUSD += parseFloat(fbuyprice.replace(/,/g, ""));
                    if (fsellnovat)
                        sumSellNoVatUSD += parseFloat(fsellnovat.replace(/,/g, ""));
                    if (fsellwithvat)
                        sumSellWithVatUSD += parseFloat(fsellwithvat.replace(/,/g, ""));
                }
            }
            else {
                if (currencyType == 2) {
                    if (fbuyprice)
                        sumBuyPriceVND -= parseFloat(fbuyprice.replace(/,/g, ""));
                    if (fsellnovat)
                        sumSellNoVatVND -= parseFloat(fsellnovat.replace(/,/g, ""));
                    if (fsellwithvat)
                        sumSellWithVatVND -= parseFloat(fsellwithvat.replace(/,/g, ""));
                }
                else {
                    if (fbuyprice)
                        sumBuyPriceUSD -= parseFloat(fbuyprice.replace(/,/g, ""));
                    if (fsellnovat)
                        sumSellNoVatUSD -= parseFloat(fsellnovat.replace(/,/g, ""));
                    if (fsellwithvat)
                        sumSellWithVatUSD -= parseFloat(fsellwithvat.replace(/,/g, ""));
                }
            }
            //Display Sum
            $("#buyPrice_USD").text(formatNumber(sumBuyPriceUSD));
            $("#sellNoVAT_USD").text(formatNumber(sumSellNoVatUSD));
            $("#sellWithVAT_USD").text(formatNumber(sumSellWithVatUSD));
            $("#buyPrice_VND").text(formatNumber(sumBuyPriceVND));
            $("#sellNoVAT_VND").text(formatNumber(sumSellNoVatVND));
            $("#sellWithVAT_VND").text(formatNumber(sumSellWithVatVND));

            //Calc TOTAL
            var ratVal = parseFloat($("#txtRates").val().replace(/,/g, ""));
            var totalBuy = ratVal * sumBuyPriceUSD + sumBuyPriceVND;
            var totalSellNoVAT = ratVal * sumSellNoVatUSD + sumSellNoVatVND;
            var totalSellWithVAT = ratVal * sumSellWithVatUSD + sumSellWithVatVND;
            $("#totalBuy").text(formatNumber(totalBuy));
            $("#totalSellNoVAT").text(formatNumber(totalSellNoVAT));
            $("#totalSellWithVAT").text(formatNumber(totalSellWithVAT));
        }
        function doBack() {

            var vnpage = getUrlVars()['page_number'];
            var strHref = "";
            if (vnpage)
                strHref = "LocalCharge.aspx?page_number=" + vnpage;
            else
                strHref = "LocalCharge.aspx";
            window.location.href = strHref;
        }
        function AddRow() {
            var x = document.getElementById('tableCharge');
            var new_row = x.rows[13].cloneNode(true);
            $(new_row).find('input[type=text]').val('');
            $('#tableCharge > tbody:first').append(new_row);
        }             
    </script>
    <style type="text/css">
        .thheader
        {
            font-weight: bold;
            vertical-align: middle;
            text-align: center;
            font-size: 11px;
            border: 1px solid #ccc !important;
            line-height: 7px !important; ;}
        .adminedit tbody tr td
        {
            border: 1px solid;
        }
        .bottom td
        {
            font-weight: bold;
            text-align: left;
        }
    </style>
</head>
<body>
    <div id="body-wrapper">
        <div id="header">
            <!--#include virtual="include/top.ascx"-->
        </div>
        <div id="main-content1">
            <h2>
                LOCAL CHARGES AND SURCHARGES</h2>
            <div class="clear">
            </div>
            <div>
                <asp:Label ID="lblError" CssClass="error" Text="" runat="server"></asp:Label></div>
            <div class="content-box" style="width: 60%">
                <form id="frmLocalCosts" runat="server">
                <div class="content-box-header">
                    <h3 style="cursor: s-resize;">
                    </h3>
                    <div class="clear">
                    </div>
                </div>
                <fieldset>
                    <div style="margin: 10px; font-weight: bold;">
                        Carr:
                        <asp:TextBox class="cls" ID="txtCarr" runat="server" Width="75px"></asp:TextBox>
                        Port:
                        <asp:TextBox class="cls" ID="txtPort" runat="server" Width="200px"></asp:TextBox>
                        &nbsp;&nbsp; Units
                        <asp:DropDownList ID="ddUnit" runat="server" AutoPostBack="true" OnSelectedIndexChanged="btnSearch_Click">
                            <%--<asp:ListItem Text="Select" Value=""></asp:ListItem>--%>
                            <asp:ListItem Text="20'DC" Value="20'DC"></asp:ListItem>
                            <asp:ListItem Text="40'DC" Value="40'DC"></asp:ListItem>
                            <asp:ListItem Text="20'RF" Value="20'RF"></asp:ListItem>
                            <asp:ListItem Text="40'RF" Value="40'RF"></asp:ListItem>
                        </asp:DropDownList>
                        &nbsp;&nbsp; Type
                        <asp:DropDownList ID="ddChargeType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="btnSearch_Click">
                            <%--<asp:ListItem Text="Select" Value=""></asp:ListItem>--%>
                            <asp:ListItem Text="EXP" Value="1"></asp:ListItem>
                            <asp:ListItem Text="IMP" Value="2"></asp:ListItem>
                        </asp:DropDownList>
                        <asp:TextBox ID="txtRates" runat="server" Width="100px" placeholder="Input Reate"></asp:TextBox>
                        <asp:TextBox ID="txtLocalChargeNote" runat="server" Width="501px" Visible="false"></asp:TextBox>
                        &nbsp;&nbsp;
                        <asp:Button class="button" ID="btnSearch" Style="float: right; margin: 5px; width: 70px;
                            font-weight: bold;" runat="server" Text="Search" OnClick="btnSearch_Click" />
                    </div>
                    <asp:Repeater ID="rptData" runat="server" OnItemDataBound="rptData_ItemDataBound">
                        <ItemTemplate>
                            <tr onmouseover="this.style.background='#dbf0c9'" onmouseout="this.style.background='#ffffff'"
                                lcindex='<%#Eval("Index") %>' lcid='<%#Eval("ID") %>'>
                                <td align="center">
                                    <input type="CheckBox" value='<%#Eval("Index") %>' id="cbSelect" name="cbSelect"
                                        style='<%#Eval("Code").ToString()=="LPF_DAYS"? "display:none": "cursor:pointer;" %>'
                                        class="cbCheckCalc" />
                                </td>
                                <td align="center">
                                    <%#Eval("ID").ToString() == "-1" ? "<input type='Text' style='width:50px;'>" : Eval("InnerCode") %>
                                    <%--<%# DataBinder.Eval(Container.DataItem, "InnerCode")%>--%>
                                    <asp:HiddenField ID="InnerCode" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "InnerCode")%>'>
                                    </asp:HiddenField>
                                    <asp:HiddenField ID="Code" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "Code")%>'>
                                    </asp:HiddenField>
                                </td>
                                <td align="center">
                                    <asp:TextBox ID="CurrencyType" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CurrencyType")%>'
                                        Style="display: none;" CssClass="TypeOfCurrency"></asp:TextBox>
                                    <asp:DropDownList ID="ddCurrencyType" runat="server" CssClass="ddCurrencyType" Visible='<%#Eval("Code").ToString()!="LPF_DAYS"? true:false %>'>
                                        <asp:ListItem Text="USD" Value="1"></asp:ListItem>
                                        <asp:ListItem Text="VND" Value="2"></asp:ListItem>
                                    </asp:DropDownList>
                                </td>
                                <td>
                                    <asp:TextBox ID="BuyPrice" runat="server" class='<%#Eval("Code").ToString()!="LPF_DAYS"?"BuyPrice":"" %>'
                                        Text='<%# DataBinder.Eval(Container.DataItem, "BuyPrice")%>'></asp:TextBox>
                                </td>
                                <td>
                                    <asp:TextBox ID="SellPriceNoVat" runat="server" class='<%#Eval("Code").ToString()!="LPF_DAYS"?"SellPriceNoVat":"" %>'
                                        Text='<%# DataBinder.Eval(Container.DataItem, "SellPriceNoVat")%>'></asp:TextBox>
                                </td>
                                <td>
                                    <asp:TextBox ID="SellPriceWithVat" runat="server" class='<%#Eval("Code").ToString()!="LPF_DAYS"?"SellPriceWithVat":"" %>'
                                        Text='<%# DataBinder.Eval(Container.DataItem, "SellPriceWithVat")%>'></asp:TextBox>
                                </td>
                                <td>
                                    <%#Eval("ID").ToString() == "-1" ? "<input type='button'  value='Add Rows' onclick='AddRow()' />" : ""%>
                                </td>
                            </tr>
                        </ItemTemplate>
                        <HeaderTemplate>
                            <table cellspacing="0" cellpadding="0" width="100%" border="0" class="adminlist"
                                id="tableCharge">
                                <thead>
                                    <tr>
                                        <th align="center" rowspan="2">
                                            <input type="CheckBox" id="checkAll" name="checkAll" />
                                        </th>
                                        <th align="center" rowspan="2">
                                            Name
                                        </th>
                                        <th align="center" rowspan="2">
                                            Curr
                                        </th>
                                        <th align="center" colspan="3">
                                            Charges
                                        </th>
                                        <th rowspan="2">
                                        </th>
                                    </tr>
                                    <tr>
                                        <th align="center">
                                            Buy
                                        </th>
                                        <th align="center">
                                            Sell - VAT
                                        </th>
                                        <th align="center">
                                            Sell + VAT
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                        </HeaderTemplate>
                        <FooterTemplate>
                            <%--<tr>
                                <td colspan="6">
                                    <input type="button" id="addmorePOIbutton" value="Add More POIs" onclick="insRow()" />
                                </td>
                            </tr>--%>
                            </tbody>
                            <tr class="bottom">
                                <td colspan="2" rowspan="2">
                                    Sub Total
                                </td>
                                <td>
                                    USD
                                </td>
                                <td id="buyPrice_USD">
                                </td>
                                <td id="sellNoVAT_USD">
                                </td>
                                <td id="sellWithVAT_USD">
                                </td>
                                <td>
                                </td>
                            </tr>
                            <tr class="bottom">
                                <td>
                                    VND
                                </td>
                                <td id="buyPrice_VND">
                                </td>
                                <td id="sellNoVAT_VND">
                                </td>
                                <td id="sellWithVAT_VND">
                                </td>
                                <td>
                                </td>
                            </tr>
                            <tr class="bottom">
                                <td colspan="3">
                                    Total
                                </td>
                                <td id="totalBuy">
                                </td>
                                <td id="totalSellNoVAT">
                                </td>
                                <td id="totalSellWithVAT">
                                </td>
                                <td>
                                </td>
                            </tr>
                            </TABLE>
                        </FooterTemplate>
                    </asp:Repeater>
                </fieldset>
                <div class="clear">
                </div>
                <div id="footer">
                </div>
                </form>
            </div>
        </div>
    </div>
</body>
</html>
